<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BTransitObj
 *
 * @author I062261
 */
class BTransitObj {

    const Scope_User = "user";
    const Scope_Application = "bApp";
    const Scope_Node = "node";
    const Scope_UINode = "uiNode";
    const Scope_ScreenFlow = "screenFlow";
    const Scope_Screen = "screen";
    const Scope_AppMeta = "appMeta";
    const VariableName_app = "appNode";
    const VariableName_CurrentNode = "CurrentNode";
    const VariableName_CurrentUINode = "CurrentUINode";
    const VariableName_CurrentScreenFlow = "CurrentScreenFlow";
    const VariableName_CurrentScreen = "CurrentScreen";
    const VariableName_Id = "Id";
    const VariableName_Class = "Class";
    const V_AppMeta = "appMeta";
    const V_CurrentNodeId = "currentNodeId";
    const V_CurrentScreenId = "currentScreenId";
    const V_CurrentScreenFlowId = "currentScreenFlowId";
    const V_CurrentScreenClass = "currentScreenClass";
    const V_CurrentScreenFlowClass = "currentScreenFlowClass";

    //put your code here
    protected $userNode = array();
    protected $appNode = array();
    protected $appMeta = array();
    protected $currentNode = array();
    protected $currentUINode = array();
    protected $currentScreenFlow = array();
    protected $currentScreen = array();

    //structure of transit structure
    /**
     * 
     * user can contain a lot of transit variable, user's scope of variable match to session
     * one user can only contain one appNode, one appNode can only contain one currentNode or currentUINode
     * currentNode equal to currentUINode
     * currentNode can contain only one currentScreenFlow or only one current screen
     */
    public function addTransitVariable($scope, $name, $value) {
        if ($scope == self::Scope_AppMeta) {
            $this->appMeta[$name] = $value;
        }

        if ($scope == self::Scope_User) {
            $this->userNode[$name] = $value;
        }

        if ($scope == self::Scope_Application) {
            $this->appNode[$name] = $value;
        }

        if ($scope == self::Scope_Node || $scope == self::Scope_UINode) {
            $this->currentNode[$name] = $value;
            $this->currentUINode = $this->currentNode;
        }

        if ($scope == self::Scope_Screen) {
            $this->currentScreen[$name] = $value;
        }
        if ($scope == self::Scope_ScreenFlow) {
            $this->currentScreenFlow[$name] = $value;
        }
    }

    public function getTransitVariable($scope, $name) {

        $currentUser = Blyn::app()->getCurrentUser();

        if ($scope == self::Scope_User)
            return $currentUser->getTransitVariable($name);
        else {
            if ($this->appMeta == NULL) {
                $this->appMeta = $currentUser->getTransitVariable(self::V_AppMeta);
            }

            if ($this->appNode == NULL) {
                //initialize appNode variables here
                $this->appNode = $currentUser->getTransitVariable(self::VariableName_app);
                $this->currentNode = isset($this->appNode[self::VariableName_CurrentNode]) ? $this->appNode[self::VariableName_CurrentNode] : array();
                $this->currentUINode = $this->currentNode;
                $this->currentScreen = isset($this->currentNode[self::VariableName_CurrentScreen]) ? $this->currentNode[self::VariableName_CurrentScreen] : array();
                $this->currentScreenFlow = isset($this->currentNode[self::VariableName_CurrentScreenFlow]) ? $this->currentNode[self::VariableName_CurrentScreenFlow] : array();
            }

            if ($scope == self::Scope_AppMeta) {
                return isset($this->appMeta[$name]) ? $this->appMeta[$name] : FALSE;
            }

            if ($scope == self::Scope_Application) {
                return isset($this->appNode[$name]) ? $this->appNode[$name] : FALSE;
            }
            if ($scope == self::Scope_Node || $scope == self::Scope_UINode) {
                return isset($this->currentNode[$name]) ? $this->currentNode[$name] : FALSE;
            }
            if ($scope == self::Scope_Screen) {
                return isset($this->currentScreen[$name]) ? $this->currentScreen[$name] : FALSE;
            }
            if ($scope == self::Scope_ScreenFlow) {
                return isset($this->currentScreenFlow[$name]) ? $this->currentScreenFlow[$name] : FALSE;
            }
        }
    }

    public function resetTransitVariable($scope, $name = NULL) {
        //the logic for reset user scope of variable is little different of other variable
        //user scope of variable will not be remove in user scope for unload of user scope of variable

        if ($scope == self::Scope_User) {
            if ($name == NULL) {
                foreach ($this->userNode as $key => $value) {
                    $this->userNode[$key] = array();
                }
            }
            if (isset($this->userNode[$name]))
                $this->userNode[$name] = array();
        }

        if ($scope == self::Scope_AppMeta) {
            if ($name == NULL) {
                $this->appMeta = array();
            }
            if (isset($this->appMeta[$name]))
                $this->appMeta[$name] = NULL;
        }

        if ($scope == self::Scope_Application) {
            if ($name == NULL) {
                $this->appNode = array();
                $this->currentNode = array();
                $this->currentScreen = array();
                $this->currentScreenFlow = array();
                $this->currentUINode = array();
            }
            if (isset($this->appNode[$name]))
                $this->appNode[$name] = array();
        }

        if ($scope == self::Scope_Node || $scope == self::Scope_UINode) {
            if ($name == NULL) {
                $this->currentNode = array();
                $this->currentUINode = array();
                $this->currentScreenFlow = array();
                $this->currentScreen = array();
            }
            if (isset($this->currentNode[$name])) {
                $this->currentNode[$name] = array();
            }
            $this->currentUINode = $this->currentNode;
        }

        if ($scope == self::Scope_Screen) {
            if ($name == NULL) {
                $this->currentScreen = array();
            }
            if (isset($this->currentScreen[$name]))
                $this->currentScreen[$name] = array();
        }
        if ($scope == self::Scope_ScreenFlow) {
            if ($name == NULL) {
                $this->currentScreenFlow = array();
            }
            if (isset($this->currentScreenFlow[$name]))
                $this->currentScreenFlow[$name] = array();
        }
    }

    /**
     * this function save transit variable to database
     * @param type $scope
     * @param type $name
     */
    public function uploadTransitVariable() {

        $currentUser = Blyn::app()->getCurrentUser();

        if ($this->currentScreen != NULL)
            $this->currentNode[self::VariableName_CurrentScreen] = $this->currentScreen;

        if ($this->currentScreenFlow != NULL)
            $this->currentNode[self::VariableName_CurrentScreenFlow] = $this->currentScreenFlow;

        if ($this->currentNode != NULL) {
            $this->appNode[self::VariableName_CurrentNode] = $this->currentNode;
            $this->currentUINode = $this->currentNode;
            //$this->appNode[self::VariableName_CurrentUINode] = $this->currentUINode;
        }

        if ($this->appNode != NULL)
            $this->userNode[self::VariableName_app] = $this->appNode;

        $this->userNode[self::V_AppMeta] = $this->appMeta;

        foreach ($this->userNode as $name => $value) {
            if ($value != NULL) {
                $currentUser->addTransitVariable($name, $value);
            }
        }
    }

    //this function will delete variables from database store
    public function unloadTransitVariables($scope, $name = NULL) {

        $currentUser = Blyn::app()->getCurrentUser();

        if ($scope == self::Scope_User) {
            if ($name == NULL) {
                foreach ($this->userNode as $name => $variable) {
                    $currentUser->resetTransitVariable($name);
                }
            } else {
                $currentUser->resetTransitVariable($name);
            }
        } else {
            $this->resetTransitVariable($scope, $name);
            $this->uploadTransitVariable();
        }
    }

}

?>
